﻿using System.Drawing;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Web;

namespace NjamNjam.Web.Services
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ImageService
    {
        [OperationContract]
         public Response SacuvajSlikuRestorana(SaveImageRequest request)
        {
            try
            {
                var image = Image.FromStream(new MemoryStream(request.Image));
                var dir = HttpContext.Current.Server.MapPath("./Images/Restorani");
                var file = Path.Combine(dir, request.Id + ".png");
                image.Save(file);
                return new Response();
            }
            catch (System.Exception exc)
            {
                return new Response {ErrorMessage = exc.Message};
            }
         }

        [OperationContract]
        public Response SacuvajSlikuPonude(SaveImageRequest request)
        {
            try
            {
                var image = Image.FromStream(new MemoryStream(request.Image));
                var dir = HttpContext.Current.Server.MapPath("./Images/Ponuda");
                var file = Path.Combine(dir, request.Id + ".png");
                image.Save(file);
                return new Response();
            }
            catch (System.Exception exc)
            {
                return new Response { ErrorMessage = exc.Message };
            }
        }

        [OperationContract]
        public Response SacuvajIkonuVrsteRestorana(SaveImageRequest request)
        {
            try
            {
                var image = Image.FromStream(new MemoryStream(request.Image));
                var dir = HttpContext.Current.Server.MapPath("./Images/VrsteRestorana");
                var file = Path.Combine(dir, request.Id + ".png");
                image.Save(file);
                return new Response();
            }
            catch (System.Exception exc)
            {
                return new Response { ErrorMessage = exc.Message };
            }
        }
    }
}